var app = new Vue({
    el: "#app",
    data: {
        id: 1,
        userId: 0,
        data: {},
        bannerData: [],
        imageSrc: '/images/collect.png',
        txtDes: "收藏",
        is_collect: 0,
    },
    created() {
        this.id = $.GetRequest().id;
        if ($.getPara("userId") != "") {
            this.userId = $.getPara("userId");
            window.localStorage.setItem('userId', this.userId)
        } else {
            this.userId = window.localStorage.getItem("userId")
        }
        console.log(this.userId )
        this.getData()
    },
    methods: {
        getData() {
            let _this = this;
            $.ajax({
                type: "GET",
                url: '/api/purchase/detail',
                data: {
                    id: _this.id,
                    userId: _this.userId,
                },
                success: function(res) {
                    if (res.code == 200) {
                        _this.data = res.data;
                        _this.bannerData = JSON.parse(res.data.banner_img);
                        _this.is_collect = res.data.is_collect;
                        if (_this.is_collect == 1) {
                            _this.imageSrc = '/images/collection-active.png';
                            _this.txtDes = "已收藏"
                        } else {
                            _this.imageSrc = '/images/collect.png';
                            _this.txtDes = "收藏"
                        }
                        _this.$nextTick(function() {
                            var swiper = new Swiper('.swiper-container',{
                                autoplay: 3000,
                                loop: true,
                                pagination: '.swiper-pagination',
                                autoplayDisableOnInteraction: false,
                            })
                        })
                    } else {
                        $.toast(res.msg, "text")
                    }
                },
                error: function() {
                    $.toast("加载失败，请重试", "text")
                }
            })
        },
        collect() {
            if (this.userId && this.userId > 0) {
                let _this = this;
                if (_this.is_collect == 1) {
                    $.ajax({
                        type: "POST",
                        url: '/api/collect/cancelCollect',
                        data: {
                            userId: _this.userId,
                            type: 3,
                            objectId: _this.id
                        },
                        success: function(res) {
                            if (res.code == 200) {
                                _this.imageSrc = '/images/collect.png';
                                _this.txtDes = "收藏";
                                _this.is_collect = 0;
                                $.toast(res.msg, "text")
                            } else {
                                $.toast(res.msg, "text")
                            }
                        },
                        error: function() {
                            $.toast("收藏失败，请重试", "text")
                        }
                    })
                } else {
                    $.ajax({
                        type: "POST",
                        url: '/api/collect/collect',
                        data: {
                            userId: _this.userId,
                            type: 3,
                            objectId: _this.id
                        },
                        success: function(res) {
                            if (res.code == 200) {
                                _this.imageSrc = '/images/collection-active.png';
                                _this.txtDes = "已收藏";
                                _this.is_collect = 1;
                                $.toast(res.msg, "text")
                            } else {
                                $.toast(res.msg, "text")
                            }
                        },
                        error: function() {
                            $.toast("收藏失败，请重试", "text")
                        }
                    })
                }
            } else {
                if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                    window.webkit.messageHandlers.appLogin.postMessage(null)
                } else {
                    alert("PC浏览器-appLogin")
                }
            }
        },
        toMobile(phone) {
            var para = {};
            para.mobile = phone;
            if (/(Android)/i.test(navigator.userAgent)) {
                jsAndroid.appMobile(JSON.stringify(para))
            } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                window.webkit.messageHandlers.appMobile.postMessage(para)
            } else {
                alert("请使用移动设备拨号！")
            }
        },
        urlHref(id) {
            if (this.userId  && this.userId > 0) {
                var para = {};
                para.title = "在线留言";
                para.type = "liuyan";
                para.url = '/PageClassify/html/LeaveWords/liuyan.html?pressId=' + id;
                if (this.userId > 0) {
                    para.url = '/PageClassify/html/LeaveWords/liuyan.html?pressId=' + id + "&userId=" + this.userId
                }
                if (/(Android)/i.test(navigator.userAgent)) {
                    jsAndroid.appPush(JSON.stringify(para))
                } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                    window.webkit.messageHandlers.appPush.postMessage(para)
                } else {
                    window.location.href = para.url;
                    alert("PC浏览器")
                }
            } else {
                if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                    window.webkit.messageHandlers.appLogin.postMessage(null)
                } else {
                    alert("PC浏览器-appLogin")
                }
            }
        },
    }
})
